package user

import (
	"gorm.io/gorm"
	"shejiao/base"
	"shejiao/common/model/user"
)

var UserMatchRecorderService = new(userMatchRecorderService)

type userMatchRecorderService struct {
	orm *gorm.DB
}

func (u *userMatchRecorderService) Get(UserId int64, y, m, d int64) user.UserMatchRecorder {
	var umr user.UserMatchRecorder
	base.Mysql.Find(&umr, "`year` = ? and `month` = ? and `day` = ? and user_id = ?", y, m, d, UserId)
	if umr.Id == 0 {
		umr = user.UserMatchRecorder{Year: y, Month: m, Day: d, UserId: UserId}
		base.Mysql.Create(&umr)
	}

	return umr
}

func (u *userMatchRecorderService) DelCall(UserId int64, y, m, d int64) {
	base.Mysql.Model(&user.UserMatchRecorder{}).Where("`year` = ? and `month` = ? and `day` = ? and user_id = ?", y, m, d, UserId).Updates(map[string]interface{}{
		"usable_call_cnt": gorm.Expr("usable_call_cnt - 1"),
	})
}

func (u *userMatchRecorderService) DelChat(UserId int64, y, m, d int64) {
	base.Mysql.Model(&user.UserMatchRecorder{}).Where("`year` = ? and `month` = ? and `day` = ? and user_id = ?", y, m, d, UserId).Updates(map[string]interface{}{
		"usable_chat_cnt": gorm.Expr("usable_chat_cnt - 1"),
	})
}

// HaveBeenChatCnt 已经匹配过的聊天次数
func (u *userMatchRecorderService) HaveBeenChatCnt(UserId int64, y, m, d int64) int64 {
	var recorder user.UserMatchRecorder
	base.Mysql.Find(&recorder, "`year` = ? and `month` = ? and `day` = ? and user_id = ?", y, m, d, UserId)
	return recorder.UsableChatCnt
}

// HaveBeenCalledCnt 语音拨打匹配过的次数
func (u *userMatchRecorderService) HaveBeenCalledCnt(UserId int64, y, m, d int64) int64 {
	var recorder user.UserMatchRecorder
	base.Mysql.Find(&recorder, "`year` = ? and `month` = ? and `day` = ? and user_id = ?", y, m, d, UserId)
	return recorder.UsableCallCnt
}
